package com.ea.eadp.nexus.test.pageDefinition;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;


public class OriginIdUP_Web3 extends BasePage {
    public OriginIdUP_Web3(WebDriver driver) {
        super(driver, OriginIdUP_Web3.class);
    }

    public void setOriginId(String originId) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(originId);
        PageOperation.SendKeyToElementAfterWait(driver, "originId", originId);
    }
    public String getErrorMessageByXPath(String xpath) {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath(xpath)).getText();
    }
    public String getOriginIdInvalidError() {
        driver.switchTo().window(this.operationWindowHandle);
        String errorMessage=getErrorMessageByXPath("//div[@id='originId-status']//div");
        if (errorMessage.isEmpty()){
            errorMessage=getErrorMessageByXPath("//div[@id='originId-tooltip']");
        }
        return errorMessage;
    }
    public void validateOriginId(String OriginId, String errorMessage) throws Exception {
        log.info("Validate OriginId:" + OriginId);
        setOriginId(OriginId);
        clickBlank(".//*[@id='loginForm']/div[1]/div[1]");
        nextStep();
        TestUtil.sleep(1);
        Assert.assertEquals(getOriginIdInvalidError(), errorMessage, "Input not trigger the correct error message");
    }
    public void nextStep() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector(".btn-upgrade>span")).click();
    }
    public void clickBlank(String xpath){
        WebElement element = driver.findElement(By.xpath(xpath));
        Actions actions = new Actions(driver);
        actions.moveToElement(element).click().perform();
        TestUtil.sleep(1);
    }
}
